<?php
namespace app\restaurant\controller;
use app\AdminRest;

use app\restaurant\info\PermissionRestaurant;
use think\App;

use app\restaurant\model\Goods as model;

use think\facade\Db;


class AdminGoods extends AdminRest
{

    protected $model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:35
     * @功能说明:列表
     */
    public function goodsList(){

        $input = $this->_param;

        $dis[] = ['status','>',-1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        $map['store_id'] = 0;

        if(!empty($input['cate_id'])){

            $dis[] = ['cate_id','=',$input['cate_id']];

        }

        $where = [];

        if(!empty($input['title'])){

            $where[] = ['title','like','%'.$input['title'].'%'];

            $where[] = ['id','=',$input['title']];
        }
        //门店
        if(!empty($input['store_id'])&&empty($input['type'])){

            $map['store_id'] = $input['store_id'];

        }
        //选择
        if(!empty($input['store_id'])&&!empty($input['type'])){

            $maps[] = ['status','>',-1];

            $maps[] = ['uniacid','=',$this->_uniacid];

            $maps[] = ['store_id','=',$input['store_id']];

            $have  = $this->model->where($maps)->column('top_id');

            $dis[] = ['id','not in',$have];

        }
        //时间查询
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['create_time','between',"$start_time,$end_time"];

        }

        $data = $this->model->goodsList($dis,$where,$input['limit'],$map);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-10-12 16:06
     * @功能说明:同步商品
     */
    public function withGoods(){

        $input = $this->_input;

        if(empty($input['goods'])){

            $map[] = ['status','>',-1];

            $map[] = ['uniacid','=',$this->_uniacid];

            $map[] = ['store_id','=',$input['store_id']];

            $have  = $this->model->where($map)->column('top_id');

            $dis[] = ['id','not in',$have];

            $dis[] = ['uniacid','=',$this->_uniacid];

            $dis[] = ['store_id','=',0];

            $map[] = ['status','>',-1];

            $have  = $this->model->where($dis)->column('id');

            $res   = $this->model->withGoods($have,$input['store_id']);

        }else{

            $res = $this->model->withGoods($input['goods'],$input['store_id']);
        }

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 11:01
     * @功能说明:添加产品
     */
    public function goodsAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $p = new PermissionRestaurant($this->_uniacid);

        $auth_num = $p->getGoodsNumber();

        $have_num = $this->model->where(['uniacid'=>$this->_uniacid,'store_id'=>0])->where('status','>',-1)->count();

        if($have_num>=$auth_num){

            $this->errorMsg('商品限制数量'.$auth_num);
        }

        $res = $this->model->goodsAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:28
     * @功能说明:详情
     */
    public function goodsInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->goodsInfo($dis);

        $data = $this->model->goodsSpeList($input['id'],$data);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 13:29
     * @功能说明:编辑
     */
    public function goodsUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->goodsUpdate($dis,$input);

        $field = 'id,top_id,title,cate_id,lunch_fee,sale_num,pv,self_foods,limit_status,limit,buy_start,top,out_foods,house_foods,cover,imgs,text,update_time';

        $goods= Db::name('longbing_card_restaurant_goods')->field($field)->where($dis)->find();

        if($goods['top_id']==0){

            $dis = [

                'top_id'=>$goods['id']
            ];

            unset($goods['id']);

            unset($goods['top_id']);

            Db::name('longbing_card_restaurant_goods')->where($dis)->update($goods);

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-10 14:23
     * @功能说明:删除
     */
    public function goodsDel(){

        $input = $this->_input;

        $dis[] = ['id','in',$input['id']];

        if(isset($input['status'])){

            $update['status'] = $input['status'];

        }else{

            $update['index_show'] = $input['index_show'];

        }

        $res = $this->model->where($dis)->update($update);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2020-10-10 15:44
     * @功能说明:商品下拉框
     */
    public function goodsSelect(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1,

            'store_id'=> 0
        ];

        $data = $this->model->where($dis)->field('id,title')->order('top')->select()->toArray();

        return $this->success($data);


    }









}
